package cre.tool.core;

import cre.tool.domain.Album;
import cre.tool.domain.AlbumInfo;
import cre.tool.exception.ToolException;

import java.util.List;
import java.util.Map;

/**
 * @Author penglai
 * @Date 2017/2/17.
 */
public class CategoryInitiator {

    private DbHandler handler = new DbHandler();
    private FileReader reader = new FileReader();

    public void start(String[] args) throws ToolException {
        try {
            if (args != null && args.length > 0) {
                String arg = args[0];
                if ("sub".equals(arg)) {
                    //doSubs(args);
                } else if ("export".equals(arg)) {
                    doExport(args);
                } else if ("import".equals(arg)) {
                    doImport(args);
                } else if ("importSub".equals(arg)) {
                    doImportSubs(args);
                } else if ("album-info".equals(arg)) {
                    doAlbumInfo(args);
                } else {
                    System.out.println("do nothing");
                }
            } else {
                throw new ToolException("参数不够！");
            }
            System.out.println("operate success!");
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new ToolException("参数不够！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化二级分类
     *
     * @param args
     * @throws Exception
     */
    private void doImportSubs(String[] args) throws Exception {
        String parentId = args[1];
        checkCategory(parentId);

        List<String> subs = reader.readSubFile();
        handler.insertSubCategory(parentId, subs);
    }

    /**
     * 查询二级分类
     *
     * @param args
     */
    private void doSubs(String[] args) throws Exception {
        String parentId = args[1];
        Map<Integer, String> subMaps = handler.selectSubMaps(parentId);
        reader.writeSubMaps(subMaps);
    }

    private Map<Integer, String> doSubs2(String[] args) throws Exception {
        String parentId = args[1];
        Map<Integer, String> subMaps = handler.selectSubMaps(parentId);
        return subMaps;
    }

    /**
     * 导出一级分类下的专辑
     */
    private void doExport(String[] args) throws Exception {
        String parentId = args[1];
        String title = null;

        if (args.length >= 3) {
            title = args[2];
        }
        List<Album> albums = handler.selectByCategoryId(parentId);
        reader.writeAlbums(parentId, albums, title);
    }

    /**
     * 导入二级分类和专辑关系
     *
     * @throws Exception
     */
    private void doImport(String[] args) throws Exception {
        String categoryId = args[1];
        checkCategory(categoryId);

        boolean autoImportSub = true;

        Map<Integer, String> subMaps = doSubs2(args);
        Map<Integer, List<Integer>> categoryAlbums = reader.readFile(categoryId, subMaps, autoImportSub);
        handler.insertCategoryAlbums(categoryAlbums, categoryId);
    }

    private void doAlbumInfo(String[] args) throws Exception {
        AlbumInfo albumInfo = reader.readAlbumInfo();
        List<Album> albums = handler.selectAlbumByIn(albumInfo.getAlbums(), albumInfo.getType());

        reader.writeAlbums(albums);
    }

    protected void checkCategory(String categoryId) throws Exception {
        if (!handler.isSingle(categoryId) || !handler.isParent(categoryId)) {
            throw new ToolException("不是单行本的一级分类！");
        }
    }
}
